package leetcode.list_problem;

/**
 * 21. 合并两个有序链表【常规解】
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 */
public class MergeTwoListsFunTwo {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        while (list1 != null && list2 != null){
            if (list1.val <= list2.val){
                cur.next = list1;
                cur = cur.next;
                list1 = list1.next;
            }else {
                cur.next = list2;
                cur = cur.next;
                list2 = list2.next;
            }
        }

        if (list1 == null){
            cur.next = list2;
        }
        if (list2 == null ){
            cur.next = list1;
        }
        return dummyHead.next;
    }
}
